package com.shiroa.controller;

import com.shiroa.common.R;
import com.shiroa.entity.SysUser;
import com.shiroa.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    //    @RequiresPermissions("user:add")
//    @RequiredPerms("role:add")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping
    public R add(@RequestBody SysUser user) {
        userService.save(user);
        return R.ok();
    }

    //    @RequiresRoles("manager")
    @PreAuthorize("hasAuthority('user:del')")
    @DeleteMapping("/{roleId}")
    public R del(@PathVariable Long roleId) {
        userService.removeById(roleId);
        return R.ok();
    }

    //    @RequiresPermissions("user:update")
    @PreAuthorize("hasAuthority('user:update')")
    @PutMapping
    public R update(@RequestBody SysUser user) {
        userService.saveOrUpdate(user);
        return R.ok();
    }

    //    @RequiresPermissions("user:list")
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping
    public R getAll() {
        List<SysUser> list = userService.list();
        return R.ok(list);
    }
}
